from datetime import timedelta
import re

# 读取文件并转换第一个数值
def convert_ms_to_time(filename):
    with open(filename, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        result = []
        
        for line in lines:
            # 使用正则表达式提取第一个数字（毫秒数）
            match = re.search(r'\[(\d+),', line)
            if match:
                ms_value = int(match.group(1))
                # 转换为时分秒格式
                time_value = str(timedelta(milliseconds=ms_value))
                # 格式化为 hh:mm:ss.sss
                if '.' not in time_value:
                    time_value += ".000"
                formatted_time = time_value.split('.')[0] + '.' + time_value.split('.')[1][:3]
                result.append(f"{ms_value} ms = {formatted_time}")
        
    return result

# 使用示例
filename = 'D:/Documents/work/video/gitee/4/updated_ch.txt'  # 使用绝对路径
converted_times = convert_ms_to_time(filename)
for time_str in converted_times:
    print(time_str)
